# Adogs
# 时间: 2021/8/13 11:30
import pygame.sprite

from actor.mine_boom import Mine, Boom
from dialog.tunnel_dialog import TunnelTask2BeginDialog, TunnelTask2EndDialog, TunnelTaskFailDialog
from scene.task import BaseTask, TaskStatus


class KillJapaneseTask(BaseTask):
    """
    任务二:炸死日本人
    方法:
    1.__init__(xiao_tie, japanese_group, font)
    2.draw_task(surface, view_x, view_y)
    3.do_self_task(key_down, key_list)
    4.__creat_mine(x, y)
    5.__creat_boom(x, y)
    """
    def __init__(self, xiao_tie, japanese_group, font):
        """
        初始化函数
        :param xiao_tie: 主角人物
        :param japanese_group: 日本人组
        :param font: 字体
        """
        self.japanese_group = japanese_group
        self.xiao_tie = xiao_tie

        self.mines_group = pygame.sprite.Group()
        self.booms_group = pygame.sprite.Group()

        self.score = 0  # 任务分数
        mine = pygame.image.load("resources/picture/mine/mine.png")     # 加载地雷图片
        self.mine_image = pygame.transform.scale(mine, (40, 40))  # 将图片缩放为40*40大小
        self.boom_image = pygame.image.load("resources/picture/mine/boom.png")  # 加载爆炸图片

        self.start_dialog = TunnelTask2BeginDialog(font)
        self.win_dialog = TunnelTask2EndDialog(font)
        self.fail_dialog = TunnelTaskFailDialog(font)
        super(KillJapaneseTask, self).__init__(self.start_dialog, self.win_dialog, self.fail_dialog)

    def draw_task(self, surface, view_x, view_y):
        """
        任务细节绘制函数
        绘制地雷和爆炸效果
        :param surface: 绘制屏幕
        :param view_x: 屏幕相对x坐标
        :param view_y: 屏幕相对y坐标
        :return: 无返回值
        """
        for mine in self.mines_group:
            mine.draw(surface, view_x, view_y)

        for boom in self.booms_group:
            boom.draw(surface, view_x, view_y)

    def do_self_task(self, key_down, key_list):
        """
        任务函数
        :param key_down: 判断是否点击按键
        :param key_list: 判断是否有键被按下
        :return: 无返回值
        """
        collide_list = pygame.sprite.groupcollide(self.mines_group, self.japanese_group, True, True)

        for item in collide_list:   # 爆炸
            self.__creat_boom(item.rect.x - item.rect.width - 20, item.rect.y - item.rect.height - 20)
            self.score += 1
        if self.score >= 2 or key_list[pygame.K_z]:     # 任务成功条件
            self.task_status = TaskStatus.win_dialog
            self.xiao_tie.set_status("army")

        if key_down and key_list[pygame.K_j]:
            self.__creat_mine(self.xiao_tie.pos_x+20, self.xiao_tie.pos_y+30)

        for boom in self.booms_group:
            if boom.kill_flag():
                self.booms_group.remove(boom)  # 移除当前的爆炸

    def __creat_mine(self, x, y):
        """
        创建炸弹函数
        :param x: 炸弹x坐标
        :param y: 炸弹y坐标
        :return: 无返回值
        """
        mine = Mine(x, y, self.mine_image)
        self.mines_group.add(mine)

    def __creat_boom(self, x, y):
        """
        创建爆炸函数
        :param x: 爆炸x坐标
        :param y: 爆炸y坐标
        :return: 无返回值
        """
        boom = Boom(x, y, self.boom_image, 4, 3)
        self.booms_group.add(boom)
